import request from "@/ulits/request"
import { getUUID } from '@/ulits/uuid_token'
export default {
  state: {
    goodInfo: '',
    //游客身份
    uuid_token: getUUID()
  },
  getters: {
    //路径导航简化的数据
    categoryView(state) {
      //报错的原因：state.goodInfo初始状态为空，没有categoryView对象所以返回为undefined
      return state.goodInfo.categoryView || {}
    },
    //简化产品的数据
    skuInfo(state) {
      return state.goodInfo.skuInfo || {}
    },
    //产品售卖属性的简化
    spuSaleAttrList(state) {
      return state.goodInfo.spuSaleAttrList || {}
    }
  },
  mutations: {
    GETGOODINFO(state, data) {
      state.goodInfo = data
    }
  },
  actions: {
    //获取产品的详细信息
    async getGoodInfo(state, data) {
      const { data: res } = await request.get(`/item/${data}`)
      state.commit('GETGOODINFO', res)
    },
    //将产品添加到购物车中 /cart/addToCart/{ skuId }/{ skuNum }
    async addOrUpdateShopCart(state, data) {
      // let res = await request.post('/cart/addToCart/:skuId/:skuNum', { parmas: data })
      let res = await request.post(`/cart/addToCart/${data.skuId}/${data.skuNum}`)
      if (res.code == 200) {
        return Promise.resolve('添加成功')
      } else
        return Promise.reject(new Error('加入失败'))
    }
  }
}